<script setup>
import { ref } from 'vue'
import { getArticleService, deleteArticleService } from '@/api/article'
import { Edit, Delete } from '@element-plus/icons-vue'
import EditChannel from './components/editChannel.vue'
import { ElMessage } from 'element-plus'
const channelList = ref([])
const loading = ref(false)
const dialog = ref()

const getArticleList = async () => {
  loading.value = true
  const res = await getArticleService()
  channelList.value = res.data.data
  loading.value = false
}

getArticleList()

const onAddChannel = () => {
  dialog.value.open({})
}
const onEditChannel = (row) => {
  dialog.value.open(row)
}
const onDeleteChannel = async (row) => {
  await ElMessageBox.confirm('确认要删除吗?', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await deleteArticleService(row.id)
  ElMessage.success('成功删除')
  getArticleList()
}

const onSuccess = () => {
  getArticleList()
}
</script>

<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="onAddChannel">添加分类</el-button>
    </template>

    <el-table v-loading="loading" :data="channelList" style="width: 100%">
      <el-table-column label="序号" type="index" width="120"></el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="120">
        <template #default="{ row }">
          <el-button
            type="primary"
            plain
            :icon="Edit"
            circle
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            type="danger"
            plain
            :icon="Delete"
            circle
            @click="onDeleteChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>

      <template #empty>
        <el-empty description="没有数据"></el-empty>
      </template>
    </el-table>
  </page-container>

  <edit-channel ref="dialog" @success="onSuccess"></edit-channel>
</template>

<style lang="scss" scoped></style>
